<?php

class Google2FA
{
    static function get_secret_key()
    {
        $google2fa = new \PragmaRX\Google2FA\Google2FA();
        return $google2fa->generateSecretKey(64);
    }

    static function get_qrcode_url($key, $username)
    {

        $config = Yaf_Registry::get('config');
        $application_name = is_null($config->application->name) ? '后台系统' : $config->application->name;

        $google2fa = new \PragmaRX\Google2FA\Google2FA();
        $google2fa->setAllowInsecureCallToGoogleApis(true);
        $google2fa_data = $google2fa->getQRCodeUrl(
            $application_name,
            $username,
            $key
        );
        $qrCode = new \Arcanedev\QrCode\QrCode();
        $qrCode->setText($google2fa_data);
        $qrCode->setSize(200);

        return $qrCode->getDataUri();
    }

    static function verify($key, $code)
    {
        $google2fa = new \PragmaRX\Google2FA\Google2FA();
        return $google2fa->verifyKey($key, $code);
    }
}